package com.smilehappiness.mock.proxy;

import com.alibaba.fastjson.JSONObject;
import com.smilehappiness.mock.annotations.MockServer;
import com.smilehappiness.mock.service.IMockService;
import com.smilehappiness.utils.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 动态代理，需要注意的是，这里用到的是JDK自带的动态代理，代理对象只能是接口，不能是类
 *
 * @author smilehappiness
 */
public class ServiceProxy<T> implements InvocationHandler {

    private Class<T> interfaceType;

    public ServiceProxy(Class<T> interfaceType) {
        this.interfaceType = interfaceType;
    }

    private IMockService mockServer;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("proxy-->" + JSONObject.toJSONString(proxy));

        System.out.println("method-->" + JSONObject.toJSONString(method));

        System.out.println("args-->" + JSONObject.toJSONString(args));

        System.out.println("object-->" + Object.class.equals(method.getDeclaringClass()));

        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        }

        System.out.println("调用前，参数：{}" + args);

        String feignValue = method.getDeclaringClass().getAnnotation(MockServer.class).value();
        String commonPath = method.getDeclaringClass().getAnnotation(MockServer.class).path();
        String url = StringUtils.isBlank(commonPath) ? "" : commonPath;
        for (Annotation temp : method.getAnnotations()) {
            if (GetMapping.class.equals(temp.annotationType())) {
                url += ((GetMapping) temp).value()[0];
            }

            if (PostMapping.class.equals(temp.annotationType())) {
                url += ((PostMapping) temp).value()[0];

            }

            if (PutMapping.class.equals(temp.annotationType())) {
                url += ((PutMapping) temp).value()[0];

            }

            if (DeleteMapping.class.equals(temp.annotationType())) {
                url += ((DeleteMapping) temp).value()[0];
            }
        }

        IMockService feignMockServer = SpringUtil.getBean("feignMockServer", IMockService.class);
        return JSONObject.parseObject(feignMockServer.queryMockResponse(feignValue, url, JSONObject.toJSONString(args)), method.getReturnType());
    }


    /****
     *
     *   if (Object.class.equals(method.getDeclaringClass())) {
     *             return method.invoke(this,args);
     *         }
     *         System.out.println("调用前，参数：{}" + args);
     *         //这里可以得到参数数组和方法等，可以通过反射，注解等，进行结果集的处理
     *         //mybatis就是在这里获取参数和相关注解，然后根据返回值类型，进行结果集的转换
     *         Object result = JSON.toJSONString(args);
     *         System.out.println("调用后，结果：{}" + result);
     *         return result;
     *
     *
     */
}